package layer
{
	/**
	 * @author kings
	 * @E-mail: qywangweiqiang@126.com
	 * 2012-12-1下午08:17:36
	 * */
	import event.UIEditsEventName;

	import fl.controls.ScrollBarDirection;
	import fl.controls.TextInput;
	import fl.controls.TileList;
	import fl.controls.listClasses.CellRenderer;
	import fl.data.DataProvider;
	import fl.events.ListEvent;

	import flash.display.DisplayObject;
	import flash.display.Sprite;

	import kingsAsLib.events.EventManager;
	import kingsAsLib.events.KEvents;


	public class LayerManagePan extends Sprite
	{
		static private var _layerManagePan:LayerManagePan=new LayerManagePan();

		private var listArr:Vector.<DisplayObject>=new Vector.<DisplayObject>;

		private var list:TileList=new TileList();

		public function LayerManagePan()
		{
			super();
			drawBG();

			list.columnCount=1;
			list.columnWidth=150;
			list.rowHeight=30;
			list.setStyle('cellRenderer', LayerRenderer);
			list.direction=ScrollBarDirection.VERTICAL;
			list.setSize(150, 500);
			list.addEventListener(ListEvent.ITEM_CLICK, listItemClickHandle)

			addChild(list);

		}

		protected function listItemClickHandle(e:ListEvent):void
		{
			var layerRender:Sprite=e.item["LayerRenderer"]

			EventManager.dispatchEvent(new KEvents(UIEditsEventName.CurrentLayerChange, layerRender));
		}

		private function drawBG():void
		{
			// 
			graphics.clear();
			graphics.beginFill(0xE5E5E5);
			graphics.drawRect(0, 0, 150, 500);
			graphics.endFill();
		}

		static public function instance():LayerManagePan
		{
			return _layerManagePan;
		}

		public function addLayer(dis:DisplayObject):void
		{
			listArr.push(dis);

			update();
		}

		public function removeLayer(dis:DisplayObject):void
		{
			for (var i:int=0; i < listArr.length; i++)
			{
				if (listArr[i] == dis)
				{
					listArr.splice(i, 1);
				}
			}

			update();
		}

		private function update():void
		{
			list.dataProvider=getData();
		}

		private function getData():DataProvider
		{
			var dp:DataProvider=new DataProvider();
			for (var i:int=0; i < listArr.length; i++)
			{
				dp.addItem({LayerRenderer:listArr[i]});
			}

			return dp;
		}

	}

	//end class
}
